define(function(require){
    var main = require('/static/main.js');
    var m = new main();
    var menu = {
        isActive : false,
        settingaction : false,
        userList : [],
        levelList : [],
        MytipId:0,
        table : '',
        init:function(){
            this.bindEvent();
            this.getList();
            this.getActionList();
        },
        getList:function(){
            //获取管理员列表
            menu.table = $("#usertable").dataTable({
                "ordering" : false,
                "serverSide": true,
                "bSort": false,
                ajax: function (data, callback, settings){
                    //封装请求参数
                    var param = {};
                    param.PageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                    param.start = data.start;//开始的记录序号
                    param.PageNo = (data.start / data.length)+1;//当前页码
                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/jurisdiction/get-admin-list",
                        cache: false,  //禁用缓存
                        data: param,  //传入组装的参数
                        dataType: "json",
                        success: function (result){
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = result.total;
                            returnData.recordsFiltered = result.total;
                            returnData.data = result.data;
                            menu.userList = result.data;
                            callback(returnData);
                        }
                    });
                },
                'bLengthChange': false,
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": false,
                "autoWidth": false,
                "aoColumns": [
                    {"mDataProp": "Nickname"},
                    {"mDataProp": "Phone"},
                    {"mDataProp": "MytipLevel",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            var MytipLevelName = '';
                            switch (oData.MytipLevel){
                                case 1 :MytipLevelName = '集客多多';break;
                                case 2 :MytipLevelName = '部长';break;
                                case 3 :MytipLevelName = '销售总监';break;
                                case 4 :MytipLevelName = '总经理';break;
                                case 5 :MytipLevelName = '总裁';break;
                            }
                            $(nTd).html(MytipLevelName);
                        }},
                    {"mDataProp": "MytipId",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            $(nTd).html('<div class="option_btn"><span class="btn-setting" data-levelid="'+oData.MytipLevel+'">设置</span>\
                             </div>');
                        }
                    },
                ],
                "fnCreatedRow": function (nRow, aData, iDataIndex) {
                },
                "fnInitComplete": function (oSettings, json) {
                }
            });
        },
        getActionList:function(){
            /******************************************************************************************************************************************/
            //获取模块信息列表
            menu.action = $("#leveltable").dataTable({
                ajax: function (data, callback, settings){
                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/jurisdiction/get-level-list",
                        dataType: "json",
                        success: function (result){
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.total = result.total;
                            returnData.data = result.data;
                            menu.levelList = result.data;
                            callback(returnData);
                        }
                    });
                },
                'bLengthChange': false,
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": false,
                "autoWidth": false,
                "aoColumns": [
                    {"mDataProp": "MytipLevelId",
                        'fnCreatedCell':function(nTd, sData, oData, iRow, iCol){
                            menu.settingaction = false;
                            $(nTd).html('<label class="cr-styled"><input type="radio" name="level" class="level-radio" data-levelid="'+ oData.MytipLevelId+'" ><i class="fa"></i></label>');
                        }
                    },
                    {"mDataProp": "LevelName"},
                ]
            });
        },
        bindEvent:function(){
            //设置
            $(document).on('click','#usertable .btn-setting',function(){
                $("#leveltable").show();
                $(".map-btn").show();
                var index = $(this).closest("tr").index();
                var data = menu.userList[index];
                menu.MytipId  = data.MytipId;
                var MytipLevel  = data.MytipLevel;
               $(".level-radio").each(function(k,v){
                    var levelid = parseInt($(this).data("levelid"));
                   if(MytipLevel == levelid){
                       $(this).click();
                   }

               })
            });
            //保存关联
            $(document).on('click','.SaveRelationShip',function(){
                var actionList =[];
                var actiontable = $("#leveltable").dataTable();
                var tableList = actiontable.fnGetNodes(); //fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                var MytipLevel = '';
                $(tableList).each(function(k,v){
                    var obj = $(v).find("input[type='radio']");
                    if(obj.is(':checked')){
                        MytipLevel = obj.data("levelid")
                    }
                });
                var JsonData = {
                    MytipId:menu.MytipId,
                    MytipLevel:MytipLevel
                }
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: '/jurisdiction/mytip-update',
                    data: JsonData,
                    success: function (ret) {
                        if(ret.IsBizSuccess){
                            var table = $('#usertable').DataTable();
                            table.ajax.reload();
                            layer.msg('保存操作成功！');
                        }else{
                            layer.msg(ret.ErrMsg);
                        }
                    }
                });

            });
        },
        showmessage:function(_obj,isshow,str){
            if(isshow){
                _obj.html(str).show();
            }else{
                _obj.html('').hide();
            }
        },
        //重置表单
        resetFrom:  function (_obj){
            _obj.find("form").each(function (index) {
                _obj.find("form")[index].reset();
            });
            menu.showmessage(_obj.find(".error"),false);
        }
    }
    menu.init();
});
